﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Stu_sys.Entity;
using Stu_sys.Models;
using Stu_sys.utils;
using System.Text.Json;

namespace Stu_sys.Controllers
{
    public class GradeController : Controller
    {
        private readonly MysqlContext _mysqlContext;
        public GradeController(MysqlContext mysqlContext)
        { 
            _mysqlContext = mysqlContext;
        }
        public IActionResult GradeList()
        {

            return View();
        }

        [HttpGet]
        public IActionResult GetGradeList()
        {
            var attrStr = MyUtils.GetAttributeToJsonStr(typeof(GradeViewModel));
            var gradeList = _mysqlContext.Grades.Include(g=>g.User).ToList();
            var gradeViewModels= new List<GradeViewModel>();
            foreach (var item in gradeList)
            {
                var grade = new GradeViewModel { 
                    GradeId=item.GradeId, 
                    GradeName=item.GradeName,
                    UserId=item.UserId,
                    UserName=item.User.UserName,
                    CreateTime=item.CreateTime,
                    UpdateTime=item.UpdateTime
                };
                gradeViewModels.Add(grade);
            }
            var gradeStr = JsonSerializer.Serialize(gradeViewModels);
            var data = new
            {
                attr = attrStr,
                row = gradeStr
            };
            return Json((data));
        }

        [HttpPost]
        public IActionResult AddGrade(Grade grade)
        {
            if (grade.GradeName == "" )
            {
                return Json(-1);
            }
            
            if (IsExistGrade(grade))
            {
                return Json(0);
            }
            _mysqlContext.Grades.Add(grade);
            var res = _mysqlContext.SaveChanges();
            return Json(res);
        }


        [HttpPost]
        public IActionResult EditGrade(Grade grade)
        {
            if (grade.GradeName == "" )
            {
                return Json(-1);
            }
            if (IsExistGrade(grade)) {
                return Json(0);
            }
            var g = _mysqlContext.Grades.Where(g=>g.GradeId==grade.GradeId).FirstOrDefault();
            g.GradeName = grade.GradeName;


            _mysqlContext.Grades.Update(g);
            var res = _mysqlContext.SaveChanges();
            return Json(res);
        }


        [HttpPost]
        public IActionResult DelGrade(Grade grade)
        {
            if (grade == null)
            {
                return Json(-1);
            }
            var g = _mysqlContext.Grades.Single(g=>g.GradeId==grade.GradeId);
            _mysqlContext.Remove(g);
            var res = _mysqlContext.SaveChanges();
            return Json(res);
        }



        /// <summary>
        /// 年级是否已存在
        /// </summary>
        /// <param name="department"></param>
        /// <returns></returns>
        private bool IsExistGrade(Grade grade)
        {
            var grades = _mysqlContext.Grades.Where(g=>g.GradeName==grade.GradeName).ToList();
            if (grades.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
